package com.tgit.sso.paltform.util;

import com.tgit.sso.core.conf.SsoConf;
import com.tgit.sso.core.conf.SsoException;
import com.tgit.sso.paltform.central.entity.Product;
import com.tgit.sso.paltform.config.ConfigInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * ClassName: LoginUtil
 * date: 2019/11/22 14:24
 * Description: TODO
 *
 * @author fanzj@tgit.com
 * @since JDK 1.8
 */
public class LoginUtil {
    /**
     * 判断url是否属于product的domain
     * @param redirectUrl
     * @param product
     */
    public static Boolean checkUrlDomain(String redirectUrl, Product product) {
        String domain = product.getDomain();
        if (domain == null) {
            return false;
        }

        String[] arr = domain.split(SsoConf.COMMA_SEPARATOR);
        for (String item : arr) {
            if(redirectUrl.contains(item)) {
                return true;
            }
        }

        return false;
    }


    //数据为空校验
    public static void isBlank(String username, String password, String validCode) {
        if (StringUtils.isBlank(username)) {
            throw new SsoException(SsoConf.ERRORCODE.USERNAME.name());
        }
        //密码验证
        if (StringUtils.isBlank(password)) {
            throw new SsoException(SsoConf.ERRORCODE.PASSWORD.name());
        }
        //验证码验证
        if (StringUtils.isBlank(validCode)) {
            throw new SsoException(SsoConf.ERRORCODE.VALID_CODE.name());
        }
    }

    /**
     * 获取版权、公司信息
     * @return
     */
    public static String getCopyright() {
        String copyright = ConfigInfo.COPYRIGHT;
        if (copyright != null) {
            copyright = StringUtil.unicodeToString(copyright);
            if (copyright.contains("${currYear}")) {
                Calendar date = Calendar.getInstance();
                String year = String.valueOf(date.get(Calendar.YEAR));
                copyright = copyright.replace("${currYear}", year);
            }
        }

        String companyFullName = Locale.US.equals(LocaleContextHolder.getLocale()) ? ConfigInfo.COMPANY_FULL_NAME_US : ConfigInfo.COMPANY_FULL_NAME;
        if(companyFullName != null) {
            copyright += StringUtil.unicodeToString(companyFullName);
        }

        return copyright;
    }

    /**
     * 获取公告显示标志
     * @return
     */
    public static Boolean getNoticeFlag() {
        //时间格式
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        long now = System.currentTimeMillis();
        try {
            if(ConfigInfo.NOTICE_FLAG == 1 && now > SDF.parse(ConfigInfo.NOTICE_START_TIME).getTime()
                    && now < SDF.parse(ConfigInfo.NOTICE_END_TIME).getTime()) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }

    /**
     * 获取公告显示内容
     * @return
     */
    public static String getNoticeContent() {
        String content = Locale.US.equals(LocaleContextHolder.getLocale()) ? ConfigInfo.NOTICE_CONTENT_EN : ConfigInfo.NOTICE_CONTENT;
        if(content != null) {
            content = StringUtil.unicodeToString(content);
        }
        return content;
    }
}
